package cn.code.auth.authorize.web;

import cn.code.auth.authorize.constant.constant;
import cn.code.auth.authorize.dao.AuthReUserGroupMapper;
import cn.code.auth.authorize.dto.ExceptionMessage;
import cn.code.auth.authorize.dto.Message;
import cn.code.auth.authorize.model.AuthReUserGroup;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * Created by wangYuDong on 2017/5/10.
 */
@Controller
@RequestMapping("/authReUserGroup")
public class AuthReUserGroupController {
    @Resource
    private AuthReUserGroupMapper authReUserGroupMapper;

    @ResponseBody
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public Page<AuthReUserGroup> getUserList(Page<AuthReUserGroup> page) {
        page.setRecords(authReUserGroupMapper.selectPage(page, new EntityWrapper<AuthReUserGroup>()));
        return page;
    }

    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Message authReUserGroupAdd(AuthReUserGroup authReUserGroup) {
        Integer i = authReUserGroupMapper.insert(authReUserGroup);
        if (i > 0) {
            return new Message("添加成功");
        } else {
            return new ExceptionMessage("添加失败", constant.ERROR_USER + constant.ERROR_CREATE);
        }
    }

    @ResponseBody
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Message authReUserGroupDelete(@PathVariable Integer id) {
        Integer i = authReUserGroupMapper.deleteById(id);
        if (i > 0) {
            return new Message("删除成功");
        } else {
            return new ExceptionMessage("删除失败", constant.ERROR_USER + constant.ERROR_DELETE);
        }
    }

    @ResponseBody
    @RequestMapping(value = "/{id}", method = RequestMethod.PATCH)
    @ApiOperation(value = "修改", httpMethod = "PATCH")
    public Message authReUserGroupUpdate(@PathVariable Integer id, AuthReUserGroup authReUserGroup) {
        authReUserGroup.setId(id);
        Integer i = authReUserGroupMapper.updateById(authReUserGroup);
        if (i > 0) {
            return new Message("更新成功");
        } else {
            return new ExceptionMessage("更新失败", constant.ERROR_USER + constant.ERROR_UPDATE);
        }
    }
}
